﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kxcxw.Data;
using kxcxw.Data.Entities;
using kxcxw.Data.Common;
using System.Web.Security;
using Webdiyer.WebControls.Mvc;

namespace KXCXW.Web.Controllers
{
    /// <summary>
    /// 搜索
    /// </summary>
    public class SearchController : Controller
    {
        #region fields
        private KXCXWDBContext db = new KXCXWDBContext();
        private CommonRepositories comm = new CommonRepositories();
        #endregion

        public ActionResult Index(int? shopType, string Condition, string sortOrder, int? page = 1)
        {
            string sCondition = Condition.ToLower();
            var lstResult = from p in db.PublichInformations
                            where p.Title.ToLower().Contains(sCondition)
                            && p.InfoType == (shopType ?? 1)
                            && p.Status == DataConstants.Normal
                            select p;
            ViewData["resultCount"] = lstResult.Count();
            ViewData["type"] = shopType != null ? shopType.ToString() : "";
            #region 筛选信息
            double highestPrice = 0;
            if (lstResult != null && lstResult.Count() > 0)
                highestPrice = Convert.ToDouble(lstResult.Max(s => s.PresentPrice));
            ViewData["highestPrice"] = highestPrice;

            if (Request.QueryString["p"] != null)
            {
                string pvalue = Request.QueryString["p"].ToString();
                if (pvalue != "all")
                {
                    string[] arr = pvalue.Split('-');
                    decimal minPrice = Convert.ToDecimal(arr[0]);
                    lstResult = lstResult.Where(s => s.PresentPrice > minPrice);
                    if (arr[1] != "")
                    {
                        decimal maxPrice = Convert.ToDecimal(arr[1]);
                        lstResult = lstResult.Where(s => s.PresentPrice < maxPrice);
                    }
                }
            }
            if (Request.QueryString["d"] != null)
            {
                string dvalue = Request.QueryString["d"].ToString();
                if (dvalue != "all")
                {
                    string[] arr = dvalue.Split('-');
                    decimal minDiscount = Convert.ToDecimal(arr[0]);
                    if (arr.Length == 2)
                    {
                        decimal maxDiscount = Convert.ToDecimal(arr[1]);
                        lstResult = lstResult.Where(s => s.Discount >= minDiscount);
                        lstResult = lstResult.Where(s => s.Discount <= maxDiscount);
                    }
                    else
                    {
                        lstResult = lstResult.Where(s => s.Discount == minDiscount);
                    }
                }
            }
            #endregion
            #region 排序
            string[] orderarr = new string[] { "", "" };
            if (!string.IsNullOrEmpty(sortOrder))
                orderarr = sortOrder.Split('-');
            string orderFeild = "Date";
            string order = "Desc";

            orderFeild = orderarr[0];
            if (orderarr.Length == 1)
                order = "Asc";
            if (order == "Asc")
            {
                switch (orderFeild)
                {
                    case "Date":
                        lstResult = lstResult.OrderBy(s => s.PublishDate);
                        break;
                    case "Price":
                        lstResult = lstResult.OrderBy(s => s.PresentPrice);
                        break;
                    case "Discount":
                        lstResult = lstResult.OrderBy(s => s.Discount);
                        break;
                    default:
                        lstResult = (lstResult.OrderByDescending(p => p.PriorityLevel)
                        .ThenByDescending(p => p.TopDate)
                        .ThenByDescending(p => p.AlertDate)
                        .ThenByDescending(s => s.PublishDate));
                        break;
                }
            }
            else
            {
                switch (orderFeild)
                {
                    case "Date":
                        lstResult = lstResult.OrderByDescending(s => s.PublishDate);
                        break;
                    case "Price":
                        lstResult = lstResult.OrderByDescending(s => s.PresentPrice);
                        break;
                    case "Discount":
                        lstResult = lstResult.OrderByDescending(s => s.Discount);
                        break;
                    default:
                        lstResult = (lstResult.OrderByDescending(p => p.PriorityLevel)
                        .ThenByDescending(p => p.TopDate)
                        .ThenByDescending(p => p.AlertDate)
                        .ThenByDescending(s => s.PublishDate));
                        break;
                }
            }
            #endregion

            int pageSize = DataConstants.morePageSize;
            int pageIndex = page ?? 1;
            PagedList<PublichInformation> temp = lstResult.ToPagedList(pageIndex, pageSize);
            if (Request.IsAjaxRequest())
                return PartialView("_SearchList", temp);

            ViewData["ProductMenu"] = comm.getProductCategoryList();
            ViewData["dlCount"] = "0";

            return View(temp);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
